<template>
  <a-modal title-align="start" title="送样" :mask-closable="false" v-model:visible="showvisible" @cancel="showvisible = false" :width="400">
    <a-form :model="form" ref="formRef" auto-label-width>
      <a-form-item label="送样方式">
        <a-select v-model="form.sendType">
          <a-option label="送货上门" value="DOOR_DELIVERY"></a-option>
          <a-option label="快递" value="EXPRESS_DELIVERY"></a-option>
        </a-select>
      </a-form-item>
      <a-form-item label="送样日期" field="gmtSend" :rules="[{ required: true, message: '请选择送样日期' }]">
        <a-date-picker v-model="form.gmtSend" value-format="timestamp" style="width: 100%" />
      </a-form-item>
      <a-form-item label="物流商" v-if="form.sendType != 'DOOR_DELIVERY'">
        <a-input v-model="form.logistics" placeholder="请输入物流商" />
      </a-form-item>
      <a-form-item label="跟踪号" v-if="form.sendType != 'DOOR_DELIVERY'">
        <a-input v-model="form.trackingNumber" placeholder="请输入物流跟踪号" />
      </a-form-item>
    </a-form>
    <template #footer>
      <a-button class="gray-outline" @click="showvisible = false">取消</a-button>
      <a-button type="primary" :loading="loading" @click="confirm">确认</a-button>
    </template>
  </a-modal>
</template>

<script lang="ts" setup>
  import { ref, computed, onMounted } from 'vue'
  import { updateTaskSendSample, sendSampleNow } from '@/api/task'
  import { Message } from '@arco-design/web-vue'
  let props = defineProps(['modelValue', 'info'])
  let emits = defineEmits(['save', 'update:modelValue'])
  let showvisible = computed({
    get() {
      return props.modelValue
    },
    set(val) {
      emits('update:modelValue', val)
    },
  })
  const form = ref<any>({
    sendType: 'DOOR_DELIVERY',
  })
  const formRef = ref<any>(null)
  let loading = ref(false)
  const confirm = async () => {
    await formRef.value.validate()
    let params = {
      sendSample: form.value,
      taskIdList: [props.info.id],
    }
    let res: any = null
    if (props.info.sendSample?.status == 'WAIT_SEND_SAMPLE') {
      res = await sendSampleNow(params)
    } else {
      params.sendSample.status = props.info.sendSample.status
      if (params.sendSample.sendType == 'DOOR_DELIVERY') {
        params.sendSample.logistics = ''
        params.sendSample.trackingNumber = ''
      }
      res = await updateTaskSendSample(params)
    }

    if (res) {
      Message.success('送样信息更新成功')
      emits('save')
      showvisible.value = false
    }
  }

  onMounted(() => {
    form.value = {
      sendType: props.info?.sendSample?.sendType || 'DOOR_DELIVERY',
      gmtSend: props.info?.sendSample?.gmtSend || new Date().getTime(),
      logistics: props.info?.sendSample?.logistics || '',
      trackingNumber: props.info?.sendSample?.trackingNumber || '',
    }
  })
</script>

<style lang="less" scoped></style>
